package com.dyw.spzx.manager.service.lmpl;

import cn.hutool.core.lang.UUID;
import com.dyw.spzx.manager.mapper.ProductDetailsMapper;
import com.dyw.spzx.manager.mapper.ProductMapper;
import com.dyw.spzx.manager.mapper.ProductSkuMapper;
import com.dyw.spzx.manager.service.ProductService;
import com.dyw.spzx.model.dto.product.ProductDto;
import com.dyw.spzx.model.entity.product.Product;
import com.dyw.spzx.model.entity.product.ProductDetails;
import com.dyw.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    // 分页查询商品列表
    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page,limit);
        List<Product> productList = productMapper.selectByPage(productDto);
        PageInfo<Product> productPageInfo = new PageInfo<>(productList);

        return productPageInfo;
    }

    // 保存商品
    @Transactional
    @Override
    public void save(Product product) {
        // 保存商品数据
        product.setStatus(0); // 设置上架状态
        product.setAuditStatus(0); // 设置审核状态
        productMapper.insertProduct(product);

        // 保存商品sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            productSku.setSkuCode(product.getId() + "-" + UUID.randomUUID());
            productSku.setProductId(product.getId().intValue());
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            productSku.setSaleNum(0);
            productSku.setStatus(0);
            productSkuMapper.insertProductSku(productSku);
        }


        // 保存商品详情数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId().intValue());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insertProductDetails(productDetails);

    }

    // 根据id查询商品详情
    @Override
    public Product getById(Long id) {
        // 根据id查询商品数据
        Product product = productMapper.selectById(id);

        // 根据商品ID查询sku数据
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(product.getId());
        product.setProductSkuList(productSkuList);
        // 根据商品ID查询商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectProductDetails(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    // 保存修改商品
    @Transactional
    @Override
    public void updateById(Product product) {
        // 修改商品基本数据
        productMapper.updateProductById(product);

        // 修改商品的sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuMapper.updateProductSkuById(productSku);
        });

        // 修改商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectProductDetails(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updataProductDetailsById(productDetails);


    }

    // 删除商品
    @Transactional
    @Override
    public void deleteById(Long productId) {
        // 根据id删除商品基本数据
        productMapper.deleteById(productId);
        // 根据商品id删除商品的sku数据
        productSkuMapper.deleteByProductId(productId);
        // 根据商品的id删除商品的详情数据
        productDetailsMapper.deleteByProductId(productId);
    }

    // 商品审核
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1) {
            product.setAuditStatus(auditStatus);
            product.setAuditMessage("审核通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审核不通过");
        }

        productMapper.updateProductById(product);

    }

    // 商品上下架
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateProductById(product);
    }
}
